//ref: [iphone-shake-event.js] by (c) 2011 Sho Hashimoto <hashimoto@shokai.org>, https://github.com/shokai/js-iphone-shake-event
root.iPhoneShake = function () {
	this.onShake = function (fn, params) {
		(function () {
			var lastShake = 0;
			var currentShake;
			var threshold = 18;
			if (params && params.threshold > 0) {threshold = params.threshold; }
			var interval = 2;
			if (params && params.interval > 0) {interval = params.interval; }
			var acc = {};
			window.addEventListener('devicemotion', function (e) {
				acc.x = e.accelerationIncludingGravity.x;
				acc.y = e.accelerationIncludingGravity.y;
				acc.z = e.accelerationIncludingGravity.z;
			});
			var running_function = false;
			setInterval(function () {
				if (!running_function) {
					if (Math.abs(acc.x) > threshold || Math.abs(acc.y) > threshold || Math.abs(acc.z) > threshold) {
						currentShake = (new Date()) / 1000;
						if (currentShake - lastShake > interval) {
							lastShake = currentShake;
						} else {
							running_function = true;
							fn(acc);
							running_function = false;
							lastShake = 0;
						}
					}
				}
			}, 100);
		}());
	};
};